import csv

def csvdictrw(file):

    #原始数据文件去掉一些项目！！！改写文件，同名写入新文件夹,datanew\
    with open(fr'data\{file}','r',encoding='utf-8') as f:
        lt=f.readlines()
        print(lt[0])
        print(type(lt[0]))
        lt[0]=lt[0].replace(', ,',',')
        lt[0]=lt[0].replace('操作,','查看,往期,')
        lt[0] = lt[0].replace('全选,序号,查看,往期,', '序号,')
        lt[0]=lt[0].replace(r'[201-1] 行业代码(GB/T4754-2017)','行业代码')
        lt[0] = lt[0].replace('期别(年)', '期别年')
        lt[0] = lt[0].replace('期别(月)', '期别月')
        lt[0]=lt[0].replace(',\n','')
        lt[0] = lt[0].replace(r';', '')
        lt[0] = lt[0].replace(r'1-本月', '本期')
        lt[0] = lt[0].replace('(损失以“-”号记)', '')
        # lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元','本期资产处置收益千元')
        # lt[0] = lt[0].replace('上年同期资产处置收益(损失以“-”号记)千元', '上年同期资产处置收益千元')
        ltlist=lt[0].split(',')
        listlen=len(ltlist)

    file1 = open(fr'data\{file}',encoding='utf-8')
    rd = csv.DictReader(file1)

    with open(fr'datanew\{file}','w',encoding='utf-8') as f:
        f.write(lt[0])
        f.write(',\n')
        for row in rd:
            for i in range(listlen):
                f.write(row[ltlist[i]]+',')
            f.write('\n')


#csvdictrw('data1.csv')
